//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.middlegroup.subjectpay.service.ucfpay;

import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

public class UcfDigestUtils {
    public static final String DEFAULT_CHARSET_NAME = "UTF-8";

    public UcfDigestUtils() {
    }

    public static String getSignData(String signName, Map<String, String> params) {
        StringBuffer content = new StringBuffer();
        ArrayList keys = new ArrayList(params.keySet());
        Collections.sort(keys);

        for(int str = 0; str < keys.size(); ++str) {
            String key = (String)keys.get(str);
            if(!signName.equals(key)) {
                String value = (String)params.get(key);
                value = null == value?"":value;
                content.append("&" + key + "=" + value);
            }
        }

        String var7 = content.toString();
        if(isEmpty(var7)) {
            return var7;
        } else if(var7.startsWith("&")) {
            return var7.substring("&".length());
        } else {
            return var7;
        }
    }

    public static String encode(String key, String data, String type) throws GeneralSecurityException, CoderException {
        if("AES".equals(type)) {
            byte[] keyByte = Hex.decodeHex((new String(key)).toCharArray());
            Key k = AESCoder.toKey(keyByte);
            byte[] encryptData = AESCoder.encrypt(data.getBytes(), k);
            return Base64.encodeBase64String(encryptData);
        } else {
            return "MD5".equals(type)?digestMD5(data + key).toLowerCase():"";
        }
    }

    public static String decode(String key, String data, String type) throws Exception {
        byte[] keyByte = Hex.decodeHex(key.toCharArray());
        Key k = AESCoder.toKey(keyByte);
        byte[] decryptData = AESCoder.decrypt(Base64.decodeBase64(data), k);
        return new String(decryptData);
    }

    private static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static String digestMD5(String input) {
        try {
            MessageDigest e = MessageDigest.getInstance("MD5");
            byte[] bDigests = e.digest(input.getBytes("UTF-8"));
            return byte2hex(bDigests);
        } catch (Exception var3) {
            return "";
        }
    }

    private static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if(stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }
}
